package com.peppa.petshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peppa.petshop.entity.Address;
import com.peppa.petshop.entity.Customer;
import com.peppa.petshop.mapper.AddressMapper;
import com.peppa.petshop.mapper.CustomerMapper;
import com.peppa.petshop.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peppa.petshop.vo.AddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张三
 * @since 2021-06-02
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired(required = false)
    private AddressMapper addressMapper;
    @Autowired(required = false)
    private CustomerMapper customerMapper;
    @Override
    public List<AddressVO> getAddressList(Integer customerId) {
        List<AddressVO> addressVOList =  new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("customer",customerId);
        Customer customer = customerMapper.selectById(customerId);
        List<Address> list = addressMapper.selectList(queryWrapper);
        for (Address address : list) {
            System.out.println(address);
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(address,addressVO);
            addressVO.setCustomer(customer.getCustomerName());
            addressVO.setCustomerId(customerId);
            addressVOList.add(addressVO);

        }
        return addressVOList;
    }

    @Override
    public String addAddress(Address address) {
        int i = addressMapper.insert(address);
        if (i==1){
            return "true";
        }
        return "false";
    }

    @Override
    public String insert(Integer customerId, String address, Integer isdefault, String username, String phone) {
        Address addr = new Address();
        addr.setCustomer(customerId);
        addr.setAddressName(address);
        addr.setIsdefault(isdefault);
        addr.setUsername(username);
        addr.setPhone(phone);
        LocalDateTime now = LocalDateTime.now();
        addr.setCreateTime(now);
        addr.setUpdateTime(now);
        addr.setAddressId(null);
        int i = addressMapper.insert(addr);
        if (i==1){
            return "true";
        }
        return "false";
    }

    @Override
    public String addDefault(Integer addressId) {
        Address address = addressMapper.selectById(addressId);
        Integer customer = address.getCustomer();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("customer",customer);
        List<Address> list = addressMapper.selectList(queryWrapper);
        for (Address addr : list) {
            addr.setIsdefault(0);
            addressMapper.updateById(addr);
        }
        address.setIsdefault(1);
        addressMapper.updateById(address);
        return "修改成功！";
    }


}
